package com.bonc.modules.home.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bonc.datasource.annotation.DataSource;
import com.bonc.modules.home.param.SpecialActionParam;
import com.bonc.modules.home.result.ChartDataResult;
import com.bonc.modules.home.service.SpecialActionService;
import com.bonc.modules.rpt.dao.PeakShavingDao;
import com.bonc.modules.rpt.dao.SunshineActionTotDao;
import com.bonc.modules.rpt.entity.PeakShavingEntity;
import com.bonc.modules.rpt.entity.SunshineActionTotEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author jinlong
 * @date 2021/8/23
 */
@DataSource("data")
@Service
public class SpecialActionServiceImpl implements SpecialActionService {

    @Autowired
    private PeakShavingDao peakShavingDao;

    @Autowired
    private SunshineActionTotDao sunshineActionTotDao;

    @Override
    public ChartDataResult getPeakShavingChart(SpecialActionParam specialActionParam) {
        LambdaQueryWrapper<PeakShavingEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PeakShavingEntity::getDealDate, specialActionParam.getDealMonth());
        queryWrapper.eq(PeakShavingEntity::getDimId, specialActionParam.getDimId());
        queryWrapper.isNotNull(PeakShavingEntity::getAvgTotValueDecreasePercent);
        if ("1".equals(specialActionParam.getDimId())) {
            queryWrapper.eq(PeakShavingEntity::getAreaCode, "00");
            queryWrapper.eq(PeakShavingEntity::getGriddingCode, "00");
            queryWrapper.orderByAsc(PeakShavingEntity::getRegionCode, PeakShavingEntity::getAreaCode, PeakShavingEntity::getGriddingCode);
        }
        if ("0".equals(specialActionParam.getDimId())) {
            queryWrapper.orderByAsc(PeakShavingEntity::getDimName);
        }
        List<PeakShavingEntity> peakShavingEntityList = peakShavingDao.selectList(queryWrapper);
        return this.convert(peakShavingEntityList);
    }


    @Override
    public ChartDataResult getSunshineActionChart(SpecialActionParam specialActionParam) {
        LambdaQueryWrapper<SunshineActionTotEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SunshineActionTotEntity::getDealMonth, specialActionParam.getDealMonth());
        List<SunshineActionTotEntity> list = sunshineActionTotDao.selectList(queryWrapper);
        return this.sunshineActionconvert(list);
    }

    /**
     * 地市排名数据转换
     *
     * @param peakShavingEntities 数据实体列表
     * @return
     */
    private ChartDataResult convert(List<PeakShavingEntity> peakShavingEntities) {

        List<String> xAxis = peakShavingEntities.stream().map(PeakShavingEntity::getRegionDesc).collect(Collectors.toList());
        List<Number> dataList = peakShavingEntities.stream().map(peakShavingEntity -> {
            if (ObjectUtil.isNotNull(peakShavingEntity) && ObjectUtil.isNotEmpty(peakShavingEntity.getAvgTotValueDecreasePercent())) {
                return peakShavingEntity.getAvgTotValueDecreasePercent().multiply(new BigDecimal(100));
            }
            return null;
        }).collect(Collectors.toList());
        return new ChartDataResult(xAxis, dataList);
    }

    /**
     * 阳光行动据转换
     *
     * @param list 数据实体列表
     * @return
     */
    private ChartDataResult sunshineActionconvert(List<SunshineActionTotEntity> list) {

        List<String> xAxis = list.stream().map(SunshineActionTotEntity::getCityName).collect(Collectors.toList());
        List<Number> dataList = list.stream().map(entity -> {
            if (ObjectUtil.isNotNull(entity) && ObjectUtil.isNotEmpty(entity.getMom())) {
                return entity.getMom().multiply(new BigDecimal(100));
            }
            return null;
        }).collect(Collectors.toList());
        List<Number> barDataList = list.stream().map(SunshineActionTotEntity::getCount).collect(Collectors.toList());
        return new ChartDataResult(xAxis, dataList, barDataList);
    }

    @Override
    public Map<String, Object> getSunshineActionMultipleChart(SpecialActionParam specialActionParam){
        LambdaQueryWrapper<SunshineActionTotEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SunshineActionTotEntity::getDealMonth, specialActionParam.getDealMonth());
        queryWrapper.orderByDesc(SunshineActionTotEntity::getCityName);
        List<SunshineActionTotEntity> list = sunshineActionTotDao.selectList(queryWrapper);
        return this.sunshineActionMultipleConvert(list);
    }


    /**
     * 阳光行动据转换
     *
     * @param list 数据实体列表
     * @return
     */
    private Map<String, Object> sunshineActionMultipleConvert(List<SunshineActionTotEntity> list) {

        Map<String, Object> momResult = new HashMap<String, Object>();
        momResult.put("legend", "不知情同比");
        momResult.put("data", list.stream().map(entity -> {
            if (ObjectUtil.isNotNull(entity) && ObjectUtil.isNotEmpty(entity.getMom())) {
                return entity.getMom().multiply(new BigDecimal(100));
            }
            return null;
        }).collect(Collectors.toList()));
        Map<String, Object> dutyIncreaseResult = new HashMap<String, Object>();
        dutyIncreaseResult.put("legend", "有责同比");
        dutyIncreaseResult.put("data", list.stream().map(entity -> {
            if (ObjectUtil.isNotNull(entity) && ObjectUtil.isNotEmpty(entity.getDutyIncrease())) {
                return entity.getDutyIncrease().multiply(new BigDecimal(100));
            }
            return null;
        }).collect(Collectors.toList()));
        HashMap<String, Object> countResult = new HashMap<String, Object>();
        countResult.put("legend", "不知情量");
        countResult.put("data", list.stream().map(SunshineActionTotEntity::getCount).collect(Collectors.toList()));
        HashMap<String, Object> dutyCountResult = new HashMap<String, Object>();
        dutyCountResult.put("legend", "有责量");
        dutyCountResult.put("data", list.stream().map(SunshineActionTotEntity::getDutyCount).collect(Collectors.toList()));

        List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
        dataList.add(countResult);
        dataList.add(dutyCountResult);

        List<Map<String, Object>> barDataList = new ArrayList<Map<String, Object>>();
        barDataList.add(momResult);
        barDataList.add(dutyIncreaseResult);


        Map<String, Object> result = new HashMap<String, Object>();
        result.put("xAxis", list.stream().map(SunshineActionTotEntity::getCityName).collect(Collectors.toList()));
        result.put("dataList", dataList);
        result.put("barDataList", barDataList);

        return result;
    }
}
